<?php

/**
 * Aurorum - Open source digital signage
 *
 * @link http://digitalsignage.tk
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Aurorum
 * @package Aurorum
 */

/**
 * This class executes start up initialization.
 *
 * @package Aurorum
 * @subpackage Core
 */

class ADS_Bootstrapper {
    
    private static $files = array();
        
    static function addDir($path){
        if(is_dir($path)){  
            self::$files = array_merge(self::$files,glob($path.'*.php'));
        }
    }
    
    static function addFile($path){
        if(is_file($path)){
            self::$files[] = $path;
        }
    }
    
    static function autoload($class_name){
        if(!preg_match('/^ADS_([a-zA-Z]+)$/',$class_name,$class_name)){
            return false;
        }
                
        $dirs = array('core','controllers','dispatchers','models','exceptions');
        foreach ($dirs as &$dir) {
            if(file_exists(ADS_DIR . '/app/'.$dir.'/'.$class_name[1].'.php'))
            {
                require_once(ADS_DIR . '/app/'.$dir.'/'.$class_name[1].'.php');
                return true;
            }
        }
        
        return false;
    }
    
    /**
     * Inits application.
     */
    static function init(){        
        //load other dependencies        
        foreach (self::$files as $file) {
            require_once $file;
        }  
        //register autoload
        spl_autoload_register(array(self,'autoload'));
        //connect to db
        dibi::connect(array(
            'driver'   => 'mysql',
            'host'     => DB_HOST,
            'username' => DB_USER,
            'password' => DB_PASSWORD,
            'database' => DB_NAME,
            'charset'  => DB_CHARSET
        ));
        dibi::getSubstitutes()->wp = 'wp_';
        //register hooks
        ADS_Observer::registerHook('ads_player', array(
            'ADS_PlayerDispatcher', 'dispatchRequest' 
        ));
        ADS_Observer::registerHook('ads_dashboard', array(
            'ADS_DashboardDispatcher', 'dispatchRequest' 
        ));
        ADS_Observer::registerHook('ads_slide', array(
            'ADS_SlideDispatcher', 'dispatchRequest' 
        ));
        ADS_Observer::registerHook('ads_dataframe', array(
            'ADS_DataFrameDispatcher', 'dispatchRequest' 
        ));
        ADS_Observer::registerHook('ads_datasource', array(
            'ADS_DataSourceDispatcher', 'dispatchRequest' 
        ));
        ADS_Observer::registerHook('ads_settings', array(
            'ADS_SettingsDispatcher', 'dispatchRequest' 
        ));
        //register actions
        add_action("admin_init", "ads_admin_init");
        add_action('_admin_menu', array('ADS_Observer','inspectRequest'));
        add_action('admin_menu', 'ads_admin_menu');
        add_action('admin_enqueue_scripts', 'ads_enqueue_scripts');
        add_action('admin_footer', 'ads_admin_footer');
        //time zone
        date_default_timezone_set(ADS_TIMEZONE);
    }
}
